var ctx=window.daijia.ctx;
var address=window.daijia.address;
var allmap=null;   //商铺地图对象
var map2=null;
function initialize(){
		var myGeo = new BMap.Geocoder();
		myGeo.getPoint(address, function(point){
			if (point) {
				map2 = new BMap.Map("allmap");  
				map2.centerAndZoom(point, 13);
				map2.enableScrollWheelZoom();
			    
			    //右键菜单
			    var contextMenu = new BMap.ContextMenu();
				var txtMenuItem = [{
				   text:'设为商家地点',
				   callback:function(p){
					   if(map2!=null){
						   map2.clearOverlays();
					   }
				   		
				   		var memberMarker = new BMap.Marker(p);  // 创建标注
				   		map2.addOverlay(memberMarker);
		    			
		    			var gc = new BMap.Geocoder();
					    gc.getLocation(p, function(rs){
					        var addComp = rs.addressComponents;
					        $("#province option").each(function(index,element){
							  	if($(element).attr("value")==addComp.province){
							 		$(element).attr("selected","selected");
							 		$(element).change();
							 		return false;
							 	}
							});
					        $("#city option").each(function(index,element){
							  	if($(element).attr("value")==addComp.city){
							 		$(element).attr("selected","selected");
							 		$(element).change();
							 		return false;
							 	}
							});
					        $("#county option").each(function(index,element){
							  	if($(element).attr("value")==address){
							 		$(element).attr("selected","selected");
							 		return false;
							 	}
							});
					        
					        $("#address").val(addComp.street+" "+addComp.streetNumber);
					        $("#lng").val(p.lng);
					        $("#lat").val(p.lat);
					        $("#dialog_update").modal("hide");
					    });         
				   }
				}];
				
				for(var i=0; i < txtMenuItem.length; i++){
					contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
					if(i==1 || i==3) {
						contextMenu.addSeparator();
					}
				}
				map2.addContextMenu(contextMenu); 
			}else{
				$.scojs_message("地区"+address+"位置不准确,请重新编辑地区", $.scojs_message.TYPE_ERROR);
			}
		});
	}

$(document).ready(function(){
	$("#phone").blur(function(){
		var phone=$("#phone").val().trim();
		if(!(/(^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8})|((400)\d{7})|((\d{3,4})-(\d{2,8})-(\d{1,4}))|((\d{3,4})-(\d{7,8}))$/.test(phone)) || phone.length>12){
				$.scojs_message("请输入正确的电话号码,如:153****1234,4001234567,1234-12345678", $.scojs_message.TYPE_ERROR);
	    }
	});	
	$("#map_btn").click(function(){
		address=$("#county").val();
		$('#dialog_modifyCash').modal();
		setTimeout("initialize()",1000);
		
	});
	$("#createPartner").click(function(){
		if($("#name").val().trim()==''){
			$.scojs_message("请填写商家名称", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#phone").val().trim()==''){
			$.scojs_message("请填写商家电话", $.scojs_message.TYPE_ERROR);
			return;
		}
		if(!(/(^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8})|((400)\d{7})|((\d{3,4})-(\d{2,8})-(\d{1,4}))|((\d{3,4})-(\d{7,8}))$/.test($("#phone").val().trim())) || $("#phone").val().trim().length>12){
			$.scojs_message("请输入正确的电话号码,如:153****1234,4001234567,1234-12345678", $.scojs_message.TYPE_ERROR);
			return;
        }
		if($("#intro").val().trim()==''){
			$.scojs_message("请填写商家详情", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#city").val().trim()==''){
			$.scojs_message("请选择城市", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#province").val().trim()==''){
			$.scojs_message("请选择省份", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#county").val().trim()==''){
			$.scojs_message("请选择区县", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#address").val().trim()==''){
			$.scojs_message("请填写商家地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#lat").val().trim()==''){
			$.scojs_message("请重新选择地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#lng").val().trim()==''){
			$.scojs_message("请重新选择地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		var categoryAlias=$("#categoryAlias").val();
		
			$.post(ctx+'/partner/create',{name:$("#name").val(),
				phone:$("#phone").val(),categoryAlias:$("#categoryAlias").val(),
				intentId:$("#intentId").val(),address:$("#address").val(),
				city:$("#city").val(),province:$("#province").val(),
				county:$("#county").val(),lat:$("#lat").val(),lng:$("#lng").val(),
				intro:$("#intro").val(),img1:$("#img1").val(),
				img2:$("#img2").val(),img3:$("#img3").val(),img4:$("#img4").val()
				},
				function(data){
		           if(data.success){
			           window.location=ctx+"/partner/list/"+categoryAlias;
		                $.scojs_message(data.message, $.scojs_message.TYPE_OK);
			       }else{
		                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			       }
			    },'json');
			
	});
	$("#updatePartner").click(function(){
		if($("#name").val().trim()==''){
			$.scojs_message("请填写商家名称", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#phone").val().trim()==''){
			$.scojs_message("请填写商家电话", $.scojs_message.TYPE_ERROR);
			return;
		}
		if(!(/(^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8})|((400)\d{7})|((\d{3,4})-(\d{2,8})-(\d{1,4}))|((\d{3,4})-(\d{7,8}))$/.test($("#phone").val().trim())) || $("#phone").val().trim().length>12){
			$.scojs_message("请输入正确的电话号码,如:153****1234,4001234567,1234-12345678", $.scojs_message.TYPE_ERROR);
			return;
        }
		if($("#intro").val().trim()==''){
			$.scojs_message("请填写商家详情", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#city").val().trim()==''){
			$.scojs_message("请选择城市", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#province").val().trim()==''){
			$.scojs_message("请选择省份", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#county").val().trim()==''){
			$.scojs_message("请选择区县", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#address").val().trim()==''){
			$.scojs_message("请填写商家地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#lat").val().trim()==''){
			$.scojs_message("请重新选择地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		if($("#lng").val().trim()==''){
			$.scojs_message("请重新选择地址", $.scojs_message.TYPE_ERROR);
			return;
		}
		var categoryAlias=$("#categoryAlias").val();
		$.post(ctx+'/partner/update',{
			id:$("#id").val(),
			name:$("#name").val(),
			phone:$("#phone").val(),categoryAlias:$("#categoryAlias").val(),
			intentId:$("#intentId").val(),address:$("#address").val(),
			city:$("#city").val(),province:$("#province").val(),
			county:$("#county").val(),lat:$("#lat").val(),lng:$("#lng").val(),
			intro:$("#intro").val(),img1:$("#img1").val(),
			img2:$("#img2").val(),img3:$("#img3").val(),img4:$("#img4").val()
		},
		function(data){
			if(data.success){
				window.location=ctx+"/partner/list/"+categoryAlias;
				$.scojs_message(data.message, $.scojs_message.TYPE_OK);
			}else{
				$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
			}
		},'json');
		
	});
	$("#deleteImg").click(function(){
		var src=$("#shareImage_img").attr("src");
		//删除已经上传的图片
		$("#shareImage_img").attr("src","");
		$("#img1").attr("value","");
		$.scojs_message("删除成功", $.scojs_message.TYPE_OK);
	});
	$("#deleteImg2").click(function(){
		$("#shareImage_img2").attr("src","");
		$("#img2").attr("value","");
		$.scojs_message("删除成功", $.scojs_message.TYPE_OK);
	});
	$("#deleteImg3").click(function(){
		$("#shareImage_img3").attr("src","");
		$("#img3").attr("value","");
		$.scojs_message("删除成功", $.scojs_message.TYPE_OK);
	});
	$("#deleteImg4").click(function(){
		$("#shareImage_img4").attr("src","");
		$("#img4").attr("value","");
		$.scojs_message("删除成功", $.scojs_message.TYPE_OK);
	});
	//自动补全交付地点
	$("#address").AutoComplete({
	    data: ctx+"/order/getPlace",
		ajaxDataType: "json",
		ajaxParams:function(keyword){
			return {'city':$("#city option:selected").val()};
		},
	    itemHeight: 24,
	    width: "auto",
	    async: true,
	    listStyle: "custom",
	    emphasis: false,
	    matchHandler: function(keyword, data){
	        return true;
	    },
	    createItemHandler: function(index, data){
	        return "<span>"+data.address+"</span>";
	    },
	    afterSelectedHandler: function(data){
		    	$("#address").val(data.address);
		    	$("#lat").val(data.lat);
		    	$("#lng").val(data.lng);
		        markerComplete(data.longitude,data.latitude);
	    }
	}).AutoComplete('show');
	
});